/**
 * Created by IntelliJ IDEA.
 * User: mstine
 * Date: Mar 20, 2008
 * Time: 2:50:36 PM
 * To change this template use File | Settings | File Templates.
 */

//Discovering the methods of a class
def d = new Date()
d.class.methods.each{println it}
println()

//Just the names
println d.class.methods.name
println()

//Dynamically Calling Methods on a Class Using Evaluate
def binding = new Binding()
binding.setVariable("d", d)
def gs = new GroovyShell(binding)

d.class.methods.each{method ->
    if (method.name.startsWith("get")) {
        print "${method.name}: "
        gs.evaluate("d = new Date(); println d.${method.name}()")
    }
}
println()

//Use a GString to do it
d.class.methods.each{method ->
    if (method.name.startsWith("get")) {
        print "${method.name}: "
        println d."${method.name}"()
    }
}
println()

//Additional methods of a Groovy Class
class PersonX{
    String firstname
    String lastname
}

def p = new PersonX(firstname:"Matt", lastname:"Stine")
println p.class.methods.name
println()

//Check for the existence of a method
if (p.metaClass.respondsTo(p, "getFirstname")) {
    println p.getFirstname()
}

p.foo()

//GOTO: duckTyping.groovy